package com.world.cat.service.sys;

import com.world.cat.enums.FileType;
import com.world.cat.model.sys.PrivateFile;
import com.world.cat.dao.sys.PrivateFileDao;
import com.world.cat.dao.sys.UserDao;
import com.world.cat.model.sys.User;
import com.world.cat.app.utils.qiniu.QiniuConfig;
import com.world.cat.app.utils.qiniu.QiniuUtil;
import com.world.cat.app.utils.qiniu.ReturnBody;
import com.world.common.util.CollectionUtil;
import com.world.common.util.SysUtil;
import com.world.common.util.sign.MD5Util;
import com.world.core.exception.WorldValidateException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by xinyu.qiu on 2017/2/25.
 */
@Service
@Transactional
public class PrivateFileService {
    private static Logger logger = LoggerFactory.getLogger(PrivateFileService.class);

    @Resource
    private UserDao userDao;
    @Resource
    private PrivateFileDao privateFileDao;
    @Resource
    private UploadRelationService uploadRelationService;

    public PrivateFile savePrivateFile(String username, ReturnBody returnBody, String filePathPrefix, String fileName) {
        PrivateFile privateFile = new PrivateFile();
        User user = null;
        if (SysUtil.isNotEmpty(username)) {
            user = userDao.findByUsername(username);
        }
        if (user == null) {
            throw new WorldValidateException("用户不存在");
        }
        privateFile.setUsername(user.getUsername());
        privateFile.setFileName(fileName);
        privateFile.setLocalFilePathPrefix(QiniuUtil.getLocalPrivateFileFolder());
        privateFile.setLocalFilePath(filePathPrefix + fileName);
        QiniuConfig privateBucketConfig = QiniuUtil.getPrivateBucketConfig();
        privateFile.setQiniuFileBucket(privateBucketConfig.getBucketName());
        privateFile.setQiniuFilePath(filePathPrefix + fileName);

        privateFile.setMimeType(returnBody.getMimeType());
        privateFile.setFileSize(returnBody.getFsize());
        privateFile.setFileHash(returnBody.getHash());
        privateFile.setFileKey(MD5Util.md5(UUID.randomUUID().toString()) + "_" + MD5Util.md5(UUID.randomUUID().toString()));
        privateFileDao.save(privateFile);
        return privateFile;
    }

    public PrivateFile savePrivateFile(String username, ReturnBody returnBody, String filePathPrefix, String fileName, FileType fileType, String objId) {
        PrivateFile privateFile = new PrivateFile();
        User user = null;
        if (SysUtil.isNotEmpty(username)) {
            user = userDao.findByUsername(username);
        }
        if (user == null) {
            throw new WorldValidateException("用户不存在");
        }
        privateFile.setUsername(user.getUsername());
        privateFile.setFileName(fileName);
        privateFile.setLocalFilePathPrefix(QiniuUtil.getLocalPrivateFileFolder());
        privateFile.setLocalFilePath(filePathPrefix + fileName);
        QiniuConfig privateBucketConfig = QiniuUtil.getPrivateBucketConfig();
        privateFile.setQiniuFileBucket(privateBucketConfig.getBucketName());
        privateFile.setQiniuFilePath(filePathPrefix + fileName);

        privateFile.setMimeType(returnBody.getMimeType());
        privateFile.setFileSize(returnBody.getFsize());
        privateFile.setFileHash(returnBody.getHash());
        privateFile.setFileKey(MD5Util.md5(UUID.randomUUID().toString()) + "_" + MD5Util.md5(UUID.randomUUID().toString()));
        PrivateFile privateFileDb = privateFileDao.save(privateFile);
        uploadRelationService.save(fileType, objId, privateFileDb.getId());
        return privateFile;
    }

    public PrivateFile getPrivateFileByFileKey(String fileKey) {
        if (StringUtils.isBlank(fileKey)) {
            return null;
        }
        PrivateFile privateFile = privateFileDao.findByFileKey(fileKey);
        return privateFile;
    }

    public PrivateFile getPrivateFileByFileId(String fileId) {
        if (StringUtils.isBlank(fileId)) {
            return null;
        }
        PrivateFile privateFile = privateFileDao.findOne(fileId);
        return privateFile;
    }

    public List<PrivateFile> getPrivateFileListByFileKeys(Set<String> fileKeys) {
        List<PrivateFile> result = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(fileKeys)) {
            List<PrivateFile> privateFileList = privateFileDao.findByFileKeyIn(fileKeys);
            for (PrivateFile privateFile : privateFileList) {
                result.add(privateFile);
            }
        }
        return result;
    }

    public Map<String, PrivateFile> getPrivateFileMapByFileKeys(Set<String> fileKeys) {
        List<PrivateFile> privateFileList = getPrivateFileListByFileKeys(fileKeys);
        Map<String, PrivateFile> result = new HashMap<>();
        for (PrivateFile privateFile : privateFileList) {
            result.put(privateFile.getFileKey(), privateFile);
        }
        return result;
    }

    public List<PrivateFile> getPrivateFileListByIdsAndUsername(Set<Long> ids, String username) {
        List<PrivateFile> privateFileList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(ids) && SysUtil.isNotEmpty(username)) {
            List<PrivateFile> privateFiles = privateFileDao.findByIdInAndUsername(ids, username);
            privateFileList.addAll(privateFiles);
        }
        return privateFileList;
    }

    public Map<String, PrivateFile> getPrivateFileMapByIdsAndUsername(Set<Long> ids, String username) {
        List<PrivateFile> privateFileList = getPrivateFileListByIdsAndUsername(ids, username);
        Map<String, PrivateFile> result = new HashMap<>();
        for (PrivateFile privateFile : privateFileList) {
            result.put(privateFile.getId(), privateFile);
        }
        return result;
    }
}